﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Dealer : Form
    {
        public Dealer()
        {
            InitializeComponent();
        }

        private void Txt_dcode_MouseClick(object sender, EventArgs e)
        {

            try
            {
                String getMax = "SELECT ISNULL(MAX(itemcode),0) FROM Dealer";
                SqlDataReader mmdr = DbConnet.ReadTable(getMax);
                String mx = "";

                if (mmdr.Read())
                {
                    mx = mmdr[0].ToString();
                }
                mmdr.Close();

                mx = mx.Substring(mx.IndexOf("D") + 1);

                int a = (int.Parse(mx) + 1);

                Txt_dcode.Text = "D00" + a;
            }
            catch (Exception exietm)
            {
            }

        }

        private void Txt_dcode_TextChanged(object sender, EventArgs e)
        {

        }

        private void btn_Dsubmit_Click(object sender, EventArgs e)
        {

            if (Txt_dcode.Text.Trim().Length <= 0)
            {
                MessageBox.Show(new Form() { TopMost = true }, "Please Enter Dealer ID ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Txt_dcode.Select();
                Txt_dcode.Focus();
            }
            else
            {
                if (Txt_dname .Text.Trim().Length <= 0)
                {
                    MessageBox.Show(new Form() { TopMost = true }, "Please Enter Dealer Name ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Txt_dname.Select();
                    Txt_dname.Focus();
                }
                else
                {
                    if (Txt_Dcredit.Text.Trim().Length <= 0)
                    {
                        MessageBox.Show(new Form() { TopMost = true }, "Please Enter Credit period ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Txt_dname.Select();
                        Txt_dname.Focus();
                    }
                    //else
                    //{
                    //    if (txtpass.Text.Trim().Length <= 0)
                    //    {
                    //        MessageBox.Show(new Form() { TopMost = true }, "Please Enter Password ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //        txtpass.Select();
                    //        txtpass.Focus();
                    //    }
                        else
                        {
                            try
                            {

//////////////////////code	varchar(50)	Unchecked
//////////////////////name	varchar(MAX)	Unchecked
//////////////////////tel	varchar(50)	Unchecked
//////////////////////address	varchar(MAX)	Unchecked
//////////////////////creditperiod	int	Unchecked
//////////////////////createdby	varchar(50)	Unchecked
//////////////////////createddate	datetime	Unchecked
//////////////////////updatedby	varchar(50)	Checked
//////////////////////updatetddate	datetime	Checked

                                String updUser = "UPDATE dealer SET code = '" + Txt_dcode.Text.Trim() + "',name = '" + Txt_dname.Text.Trim() + "',tel = '" + Txt_Dtel.Text.Trim() + "',address = '" + Txt_Dadd.Text.Trim() + "',updatedby = '" + new Midval().getUser() + "',updatetddate = '" + DateTime.Now + "' WHERE code = '" + Txt_dcode.Text.ToString() + "'";

                                String insUser = "INSERT INTO dealer (code,name,tel,address,creditperiod,createdby,createddate) VALUES ('" + Txt_dcode.Text.Trim() + "','" + Txt_dname.Text.Trim() + "','" + Txt_Dtel.Text.Trim() + "' , '" + Txt_Dadd.Text.Trim() + "','" + Txt_Dcredit.Text.ToString() + "','" + new Midval().getUser() + "','" + DateTime.Now + "') ";


                                Boolean upd = DbConnet.AddEditDel(updUser);
                                Boolean ins = false;

                                if (!upd)
                                    ins = DbConnet.AddEditDel(insUser);

                                if (upd || ins)
                                {
                                    MessageBox.Show(new Form() { TopMost = true }, "DealerCreated/Updated Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    
                                    Txt_Dadd.Clear();
                                    Txt_dcode.Clear();
                                    Txt_Dcredit.Clear();
                                    Txt_Dtel.Clear();
                                }

                            }
                            catch (Exception exins)
                            {
                            }
                       
                    }
                }
            }


        }

        private void Btn_Dcancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void Dealer_Load(object sender, EventArgs e)
        {
            DbConnet.connect(new Log().getConstr());
        }//btn close
    }
}
